var express = require('express');
var app = express();
var fs = require("fs");
var path = require('path');
var bodyParser = require('body-parser');
app.use(bodyParser.json({
    limit: '10mb'
}))
app.use(bodyParser.urlencoded({
    limit: '10mb',
    extended: true
}))
app.use(express.static(path.join(__dirname, '/dist')));
// 注释测试好好说说
app.post('/apiHtml', function(req, res) {
    // var query = Object.assign({}, req.query, req.body);
    console.log(req.body.index);
    var htmlBox, cssBox;
    if (req.body.index == 0) {
        htmlBox =
            `<!DOCTYPE html><html><head><meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width,initial-scale=1.0">
		<title></title><link rel="stylesheet" href="css.css"></head><body><div class="content">${req.body.htmlArr.join('')}</div></body></html>`;
        cssBox =
            `body {margin: 0;}.content {width: 1200px;margin: 0 auto;}.con{position: relative;width: 1200px;background-repeat: no-repeat;background-position: center;}
		${req.body.cssArr.join('')}`;
    }
    if (req.body.index == 1) {
        htmlBox =
            `<!DOCTYPE html><html><head><meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width,initial-scale=1.0">
		<title></title><link rel="stylesheet" href="css.css"></head><script type="text/javascript">
		function setSize() {
			if (document.documentElement.clientWidth > 750) {
				document.documentElement.style.fontSize = '100px';
			} else {
				document.documentElement.style.fontSize = 100 * (document.documentElement.clientWidth / 750) + 'px';
			}
		}
		setSize();
		window.onresize = function() {
			setSize();
		}
	</script><body><div class="content">${req.body.htmlArr.join('')}</div></body></html>`;
        cssBox = `body {margin: 0;}img {width: 100%;}.content {
			margin: 0 auto;
			max-width: 750px;
			width: 100%;
		}
		${req.body.cssArr.join('')}
		.con {
			position: relative;
			background-size: cover;
			background-repeat: no-repeat;
			background-position: center;
		}`;
    }

    fs.writeFile('./dist/createFile/index.html', htmlBox, function(err) {
        if (err) {
            return console.error(err);
        }
    });
    fs.writeFile('./dist/createFile/css.css', cssBox, function(err) {
        if (err) {
            return console.error(err);
        }
    });
    res.json({
        msg: '发送成功',
        code: 200,
    });
})
var server = app.listen(8001, function() {
    var host = server.address().address
    var port = server.address().port
})